package com.chen.volatiletest;

import java.util.concurrent.TimeUnit;

// volatile的可见性
public class JMMTest {

    // 不加volatile会死循环
    private static volatile int num = 0;

    public static void main(String[] args) {

        // 该线程不知道主内存的变化
        new Thread(() -> {
            while (num == 0) {

            }
        }).start();

        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        num = 1;
        System.out.println(num);

    }

}
